package com.mobile.challenge.ui.report.my

import android.app.Application
import android.graphics.Color
import androidx.lifecycle.MutableLiveData
import com.github.mikephil.charting.data.PieData
import com.github.mikephil.charting.data.PieDataSet
import com.github.mikephil.charting.data.PieEntry


import com.mobile.challenge.api.MonthDataService
import com.mobile.challenge.api.UserOneDayDataService
import com.mobile.challenge.api.WeekDataService
import com.mobile.challenge.exception.MobileException
import com.mobile.challenge.ui.report.model.PeriodType

import com.mobile.challenge.ui.report.utils.IntegerFormat

import com.mobile.challenge.user.ClockHelper
import com.mobile.challenge.user.UserHelper
import com.mobile.challenge.util.DateUtil
import com.mobile.challenge.util.SUCCESS_ERROR_CODE
import com.mobile.challenge.util.SUCCESS_ERROR_MSG
import com.mobile.frame.mvvmframe.base.BaseModel
import com.mobile.frame.mvvmframe.base.DataViewModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.*
import javax.inject.Inject
import kotlin.collections.ArrayList

class MyReportViewModel @Inject constructor(application: Application, model: BaseModel?) : DataViewModel(application, model) {

    var pieData = MutableLiveData<PieData>()
    var totalCount = 0


    fun updateData(periodType: Int, calendar: Calendar) {
        when (periodType) {
            PeriodType.DAILY -> {
                generateDailyReport(calendar)
            }
            PeriodType.WEEKLY -> {
                generateWeeklyReport(calendar)
            }
            PeriodType.MONTH -> {
                generateMonthlyReport(calendar)
            }
        }
    }

    /**
     * 生成天的数据
     */
    private fun generateDailyReport(calendar: Calendar) {

        val subscribe = getRetrofitService(UserOneDayDataService::class.java)
                .getUserDataByOneDay(UserHelper.getInstance().getUserId(),
                        DateUtil.getFormatDateStr(calendar))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ response ->
                    if (response.errorCode == SUCCESS_ERROR_CODE && response.errorMsg == SUCCESS_ERROR_MSG) {
                        val data = response.data
                        // 数据
                        val myReportList = ArrayList<PieEntry>(0).apply {
                            add(PieEntry(1F, ClockHelper.getInstance().getClockString(data.status)))
                        }
                        totalCount = 1
                        updateLiveData(myReportList)

                    } else {
                        val exception = MobileException()
                        exception.errorCode = response.errorCode
                        exception.errorMsg = response.errorMsg
                        updateLiveData(ArrayList(0))
                        totalCount = 0
                    }
                }, {
                    val exception = MobileException()
                    exception.errorCode = "00000"
                    exception.errorMsg = "response.errorMsg"
                    updateLiveData(ArrayList(0))
                    totalCount = 0
                })

    }

    /**
     * 生成周数据
     */
    private fun generateWeeklyReport(calendar: Calendar) {
        val subscribe = getRetrofitService(WeekDataService::class.java)
                .getData(UserHelper.getInstance().getUserId(),
                        DateUtil.getFormatWeekStartData(calendar),
                        DateUtil.getFormatDateStr(calendar))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ response ->
                    if (response.errorCode == SUCCESS_ERROR_CODE && response.errorMsg == SUCCESS_ERROR_MSG) {
                        val data = response.data
                        // 数据
                        val myReportList = ArrayList<PieEntry>(0).apply {
                            if (data.nacount > 0) {
                                add(PieEntry(data.nacount.toFloat(), "NA"))
                            }

                            if (data.officeCount > 0) {
                                add(PieEntry(data.officeCount.toFloat(), "Office"))
                            }

                            if (data.leaveCount > 0) {
                                add(PieEntry(data.leaveCount.toFloat(), "Vacation"))
                            }

                            if (data.workHomeCount > 0) {
                                add(PieEntry(data.workHomeCount.toFloat(), "WFH"))
                            }
                        }
                        totalCount = data.total
                        updateLiveData(myReportList)

                    } else {
                        val exception = MobileException()
                        exception.errorCode = response.errorCode
                        exception.errorMsg = response.errorMsg
                        updateLiveData(ArrayList(0))
                        totalCount = 0
                    }

                }, {
                    val exception = MobileException()
                    exception.errorCode = "00000"
                    exception.errorMsg = "response.errorMsg"
                    updateLiveData(ArrayList(0))
                    totalCount = 0
                })
    }

    /**
     * 生成月数据
     */
    private fun generateMonthlyReport(calendar: Calendar) {
        val subscribe = getRetrofitService(MonthDataService::class.java)
                .getData(UserHelper.getInstance().getUserId(),
                        DateUtil.getFormatDateMonthStr(calendar))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ response ->
                    if (response.errorCode == SUCCESS_ERROR_CODE && response.errorMsg == SUCCESS_ERROR_MSG) {
                        val data = response.data
                        // 数据
                        val myReportList = ArrayList<PieEntry>(0).apply {
                            if (data.nacount > 0) {
                                add(PieEntry(data.nacount.toFloat(), "NA"))
                            }

                            if (data.officeCount > 0) {
                                add(PieEntry(data.officeCount.toFloat(), "Office"))
                            }

                            if (data.leaveCount > 0) {
                                add(PieEntry(data.leaveCount.toFloat(), "Vacation"))
                            }

                            if (data.workHomeCount > 0) {
                                add(PieEntry(data.workHomeCount.toFloat(), "WFH"))
                            }
                        }
                        totalCount = data.total
                        updateLiveData(myReportList)

                    } else {
                        val exception = MobileException()
                        exception.errorCode = response.errorCode
                        exception.errorMsg = response.errorMsg
                        updateLiveData(ArrayList(0))
                        totalCount = 0
                    }

                }, {
                    val exception = MobileException()
                    exception.errorCode = "00000"
                    exception.errorMsg = "response.errorMsg"
                    updateLiveData(ArrayList(0))
                    totalCount = 0
                })
    }


    private fun updateLiveData(myReportList: ArrayList<PieEntry>) {
        val dataSet = PieDataSet(myReportList, "My Report")
        // 颜色数据
        val dataColors = ArrayList<Int>(0).apply {
            add(Color.parseColor("#f38181"))
            add(Color.parseColor("#fce38a"))
            add(Color.parseColor("#eaffd0"))
            add(Color.parseColor("#95e1d3"))
        }
        dataSet.setDrawValues(true)
        dataSet.colors = dataColors
        dataSet.valueTextSize = 14F
        dataSet.valueFormatter = IntegerFormat()


        pieData.value = PieData(dataSet)
    }

}